<?php

class TestModuleModule extends fvModule {

    function __construct () {
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showIndex() {
        if (!fvRequest::getInstance()->hasRequestParameter("page")) {
            if (fvSite::$fvSession->is_set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page")) {
                fvRequest::getInstance()->putRequestParameter("page",
                    (int)fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/page"));
            } 
        } else {
            fvSite::$fvSession->set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page", 
                fvRequest::getInstance()->getRequestParameter("page"));
        }

        if (substr($requestURL = fvRequest::getInstance()->getRequestParameter("requestURL"), 0, 1) == "/") {
            $requestURL = substr($requestURL, 1);
        }
        fvSession::set('question_id', 0);
        $this->__assign('TestModules', TestModuleManager::getInstance()->getAll());
        return $this->__display('testmodule_list.tpl');    
    }
    
    function showEdit() {
        $request = fvRequest::getInstance();
        if (!$TestModule = TestModuleManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $TestModule = new TestModule();
        }
        $this->__assign(array(
        'TestModule' => $TestModule,
        ));
        return $this->__display('testmodule_edit.tpl');
    }
    
    function showNext() {
    	$request = fvRequest::getInstance();
        if (!$TestModule = TestModuleManager::getInstance()->getByPk(fvSession::get('test_id'))) {
            $TestModule = new TestModule();
        }
    	if (!$Question = QuestionManager::getInstance()->getAll('testmodule_id='.fvSession::get('test_id'))) {
            $Question = new Question();
        }
//        echo "<pre>";print_r($Question);echo "</pre>";
        $this->__assign(array(
	        'TestModule' => $TestModule,
	        'Question' => $Question[fvSession::get('question_id')],
	        'question_id' => fvSession::get('question_id')
        ));
        return $this->__display('testmodule_next.tpl');
    }
    
    function showFinish() {
    	$request = fvRequest::getInstance();
        if (!$TestModule = TestModuleManager::getInstance()->getByPk(fvSession::get('test_id'))) {
            $TestModule = new TestModule();
        }
        if (!$Question = QuestionManager::getInstance()->getAll('testmodule_id='.fvSession::get('test_id'))) {
            $Question = new Question();
        }
        $answers = fvSession::get('answers');
//        echo "<pre>";
//        var_dump($quest);
//        echo "</pre>";
		$correct_answers = 0;
		var_dump($answers);
        foreach ($Question as $key=>$value)
        {
        	if ($Question[$key]['answers'][$answers[$key+1]-1]['correct'])
        	{
        		$correct_answers++;
        	}
//        	echo $answers[$key+1] . '->' . $quest['questions'][$key]['answers'][$answers[$key+1]-1]['correct'] ."<br>";
        }
        $test_result = round($correct_answers / 10 * 100);
    	$UserLevel = "Новичок";
        	if($test_result > 90){
        		$UserLevel = "Мастер – Волшебник";
        	}
        	else if($test_result > 80){
        		$UserLevel = "Эксперт";
        	}
        	else if($test_result > 60){
        		$UserLevel = "Профессионал";
        	}
        	else if($test_result > 40){
        		$UserLevel = "Знаток ";
        	}
        	else if($test_result > 20){
        		$UserLevel = "Подмастерье";
        	}
        $this->__assign(array(
	        'TestModule' => $TestModule,
	        'CountQuestions' => count($quest['questions']),
	        'CorrectAnswers' => $correct_answers,
	        'TestResult' => $test_result,
	        'UserLevel' => $UserLevel
        ));
        return $this->__display('testmodule_finish.tpl');
    }
    
//    function showStart() {
//    	$request = fvRequest::getInstance();
//        if (!$TestModule = TestModuleManager::getInstance()->getByPk(fvSession::get('test_id'))) {
//            $TestModule = new TestModule();
//        }
//        if (!$Question = $TestModule->get('data')) {
//            $Question = new Question();
//        }
//        echo fvSession::get('question_id');
//        $this->__assign(array(
//        'TestModule' => $TestModule,
//        'Question' => $Question,
//        'question_id' => fvSession::get('question_id')
//        ));
//        return $this->__display('testmodule_next.tpl');
//    }
    
}

?>
